{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "is_executing": true
   },
   "outputs": [],
   "source": [
    "import httpx\n",
    "import pandas as pd\n",
    "\n",
    "response = httpx.post('https://m.suanya.com/restapi/soa2/14666/json/GetBookingByStationV3ForPC',\n",
    "                      headers={\n",
    "                          \"authority\": \"m.suanya.com\",\n",
    "                          \"accept\": \"*/*\",\n",
    "                          \"accept-language\": \"zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6\",\n",
    "                          \"cache-control\": \"max-age=0\",\n",
    "                          \"content-type\": \"application/x-www-form-urlencoded; charset=UTF-8\",\n",
    "                          \"if-modified-since\": \"Thu, 01 Jan 1970 00:00:00 GMT\",\n",
    "                          \"origin\": \"https://www.suanya.com\",\n",
    "                          \"referer\": \"https://www.suanya.com/\",\n",
    "                          \"sec-ch-ua\": '\"Not A(Brand\";v=\"99\", \"Microsoft Edge\";v=\"121\", \"Chromium\";v=\"121\"',\n",
    "                          \"sec-ch-ua-mobile\": \"?0\",\n",
    "                          \"sec-ch-ua-platform\": '\"Windows\"',\n",
    "                          \"sec-fetch-dest\": \"empty\",\n",
    "                          \"sec-fetch-mode\": \"cors\",\n",
    "                          \"sec-fetch-site\": \"same-site\"\n",
    "                      },\n",
    "                      data={\n",
    "                          \"ArriveStation\": \"上海\",\n",
    "                          \"ChannelName\": \"ctrip.pc\",\n",
    "                          \"DepartDate\": \"2024-02-19\",\n",
    "                          \"DepartStation\": \"赣榆\"\n",
    "                      }\n",
    "                      )\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [],
   "source": [
    "# 使用orgjson格式化输出 \n",
    "import orjson\n",
    "\n",
    "text = response.text\n",
    "data = orjson.loads(text)\n",
    "TrainItems = data[\"ResponseBody\"][\"TrainItems\"]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-05T13:40:36.489867700Z",
     "start_time": "2024-02-05T13:40:36.467598500Z"
    }
   },
   "id": "1b5777335e82ca4d"
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "data": {
      "text/plain": "      车次   开始时间   结束时间    耗时  一等座价格  一等座余票量  二等座价格  二等座余票量\n0  D2131  08:41  12:41   4.0    352       0    221       0\n1  D2923  10:11  14:41  4.30    372       0    232       0\n2  D2135  10:49  15:10  4.21    352       0    220       0\n3  G7585  13:03  17:12   4.9    463       0    290       0\n4  D2145  15:50  19:09  3.19    371       0    233       0\n5  D2927  18:22  22:38  4.16    399       0    250       0\n6  D2151  18:34  22:32  3.58    371       0    233       0\n7  D4117  19:41  00:23  4.42    371      99    233      99",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>车次</th>\n      <th>开始时间</th>\n      <th>结束时间</th>\n      <th>耗时</th>\n      <th>一等座价格</th>\n      <th>一等座余票量</th>\n      <th>二等座价格</th>\n      <th>二等座余票量</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>D2131</td>\n      <td>08:41</td>\n      <td>12:41</td>\n      <td>4.0</td>\n      <td>352</td>\n      <td>0</td>\n      <td>221</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>D2923</td>\n      <td>10:11</td>\n      <td>14:41</td>\n      <td>4.30</td>\n      <td>372</td>\n      <td>0</td>\n      <td>232</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>D2135</td>\n      <td>10:49</td>\n      <td>15:10</td>\n      <td>4.21</td>\n      <td>352</td>\n      <td>0</td>\n      <td>220</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>G7585</td>\n      <td>13:03</td>\n      <td>17:12</td>\n      <td>4.9</td>\n      <td>463</td>\n      <td>0</td>\n      <td>290</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>D2145</td>\n      <td>15:50</td>\n      <td>19:09</td>\n      <td>3.19</td>\n      <td>371</td>\n      <td>0</td>\n      <td>233</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>D2927</td>\n      <td>18:22</td>\n      <td>22:38</td>\n      <td>4.16</td>\n      <td>399</td>\n      <td>0</td>\n      <td>250</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>D2151</td>\n      <td>18:34</td>\n      <td>22:32</td>\n      <td>3.58</td>\n      <td>371</td>\n      <td>0</td>\n      <td>233</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>D4117</td>\n      <td>19:41</td>\n      <td>00:23</td>\n      <td>4.42</td>\n      <td>371</td>\n      <td>99</td>\n      <td>233</td>\n      <td>99</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用列表推导式和字典推导式提取所需信息\n",
    "data = [\n",
    "    {\n",
    "        \"车次\": item[\"TrainName\"],\n",
    "        \"开始时间\": item[\"StartTime\"],\n",
    "        \"结束时间\": item[\"EndTime\"],\n",
    "        \"耗时\": f'{item[\"UseTime\"] // 60}.{item[\"UseTime\"] % 60}',\n",
    "        \"一等座价格\": next(\n",
    "            (ticket[\"Price\"] for ticket in item[\"TicketResult\"][\"TicketItems\"] if ticket[\"SeatTypeName\"] == \"一等座\"),\n",
    "            None),\n",
    "        \"一等座余票量\": next((ticket[\"Inventory\"] for ticket in item[\"TicketResult\"][\"TicketItems\"] if\n",
    "                              ticket[\"SeatTypeName\"] == \"一等座\"), None),\n",
    "        \"二等座价格\": next(\n",
    "            (ticket[\"Price\"] for ticket in item[\"TicketResult\"][\"TicketItems\"] if ticket[\"SeatTypeName\"] == \"二等座\"),\n",
    "            None),\n",
    "        \"二等座余票量\": next((ticket[\"Inventory\"] for ticket in item[\"TicketResult\"][\"TicketItems\"] if\n",
    "                              ticket[\"SeatTypeName\"] == \"二等座\"), None),\n",
    "    }\n",
    "    for item in TrainItems\n",
    "]\n",
    "\n",
    "# 创建DataFrame\n",
    "df = pd.DataFrame(data)\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-05T13:40:36.494854400Z",
     "start_time": "2024-02-05T13:40:36.470887Z"
    }
   },
   "id": "cb3681e246bd8672"
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-05T13:40:36.494854400Z",
     "start_time": "2024-02-05T13:40:36.482663700Z"
    }
   },
   "id": "f178ab818c91a0b8"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
